<!doctype html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="//cdn.bootcss.com/vue/1.0.17/vue.min.js"></script>
  <script src="//cdn.bootcss.com/vue-resource/0.7.0/vue-resource.min.js"></script>
</head>
<body>
<div id="app">
  <form  @submit.prevent="onSubmit">
    <input type="text" name="name" v-model="name">
    <input type="password" name="password" v-model="password">
    <input type="submit" value="登录">
  </form>
  <p></p>
  <input type="text" id="token" v-model="token">
  <button @click="onGetApi">填写token访问api</button>
  <p></p>
  请求结果:
  <pre>@{{ msg | json }}</pre>
</div>
<script>
  new Vue({
    el: "#app",
    data: {
      name: 'admin',
      password: 'admin',
      token: '',
      msg: ''
    },
    methods: {
      onSubmit: function(e) {
        this.$http.post('/login', {name: this.name, password: this.password}).then(
          function(response){
            this.msg = response
            this.token = response.data.token
          },
          function(response){
            this.msg = response
          }
        )
      },
      onGetApi: function () {
        this.$http.get('/api', null, {
          headers: {Authorization: 'Bearer ' + this.token}
        }).then(function(response){
          this.msg = response
        }, function (response){
          this.msg = response
        });
      }
    }
  })
</script>
</body>
</html>